package com.sanpu.kxe.common.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.utils.ScreenUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description：公共工具类
 * Created by 薛勇军 on 2018/6/27.
 */

public class CommonUtils {

    /**
     * 把隐藏掉完成银行卡号
     *
     * @return
     */
    public static String secretBankNumber(String bankNo) {
        if (TextUtils.isEmpty(bankNo)) return "";
        try {
            CharSequence secret = bankNo.subSequence(bankNo.length() - 4, bankNo.length());
            return "************" + secret;
        } catch (Exception e) {
            e.printStackTrace();
            return bankNo;
        }
    }

    /**
     * 判断卡类型
     *
     * @return
     */
    public static String checkBankCardType(String cardType) {
        if (TextUtils.equals(CommonConfig.CREDIT_CARD_TYPE, cardType)) {
            return CommonConfig.CREDIT_CARD;
        } else {
            return CommonConfig.DEPOSIT_CARD;
        }
    }

    /**
     * 名字隐藏替换
     *
     * @return
     */
    public static String secretIdName(String name) {
        if (TextUtils.isEmpty(name)) return "";
        try {
            CharSequence secret = name.subSequence(0, name.length() - 1);
            return name.replace(secret, secret.length() == 1 ? "*" : "**");
        } catch (Exception e) {
            e.printStackTrace();
            return name;
        }
    }

    /**
     * 身份证隐藏替换
     */
    public static String secretIdNumber(String number) {
        if (TextUtils.isEmpty(number)) return "";
        try {
            CharSequence start = number.subSequence(0, 4);
            CharSequence end = number.subSequence(number.length() - 4, number.length());

            return start + "**********" + end;
        } catch (Exception e) {
            e.printStackTrace();
            return number;
        }
    }

    /**
     * 手机号码隐藏替换
     */
    public static String secretPhoneNumber(String number) {
        if (TextUtils.isEmpty(number)) return "";
        try {
            CharSequence start = number.subSequence(0, 3);
            CharSequence end = number.subSequence(number.length() - 4, number.length());

            return start + "****" + end;
        } catch (Exception e) {
            e.printStackTrace();
            return number;
        }
    }

    /**
     * 验证是否是数字
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 分段格式化银行卡号码(xxxx-xxxx-xxxx-xxxx-xxx格式)
     */
    public static String bankNumFormatter(String number) {
        String ret;
        int len = number.length();

        if (len <= 4) {
            ret = number;
        } else if (len <= 8) {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, len);
            ret = s1 + " " + s2;
        } else if (len <= 12) {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, 8);
            String s3 = number.substring(8, len);
            ret = s1 + " " + s2 + " " + s3;
        } else if (len <= 16) {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, 8);
            String s3 = number.substring(8, 12);
            String s4 = number.substring(12, len);
            ret = s1 + " " + s2 + " " + s3 + " " + s4;
        } else {
            String s1 = number.substring(0, 4);
            String s2 = number.substring(4, 8);
            String s3 = number.substring(8, 12);
            String s4 = number.substring(12, 16);
            len = len < 19 ? len : 19;
            String s5 = number.substring(16, len);
            ret = s1 + " " + s2 + " " + s3 + " " + s4 + " " + s5;
        }
        return ret;
    }

    /**
     * 获取分享文案
     */
    public static String[] getShareSummary(Context context) {
        UserConfigOutput configOutput = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
        if (configOutput == null) {
            return new String[]{context.getString(R.string.share_title), context.getString(R.string.share_summary)};
        }
        return configOutput.getShareSummary();
    }

    /**
     * 获取userConfig配置的分享图标
     */
    public static String getShareIcon() {
        UserConfigOutput configEntity = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
        if (configEntity != null) {
            return UrlConstants.getBaseUrl() + configEntity.getShareIcon();
        }
        return null;
    }

    /**
     * 根据图片的url路径获得Bitmap对象
     *
     * @param url
     * @return
     */
    public static Bitmap returnBitmap(String url) {
        String pic = "";
        try {
            pic = URLDecoder.decode(url, "UTF-8").replaceAll("\\r", "").replaceAll("\\n", "");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] bytes = Base64.decode(pic, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    /**
     * 根据payType获取图片资源
     */
    public static int getPayDrawable(String tradeSource) {
        if (TextUtils.isEmpty(tradeSource)) {
            return R.drawable.shape_placeholder;
        }
        switch (tradeSource) {
            case "PERSON_COLLECTION":
                return R.drawable.ic_trade_personal;
            case "MERCHANT_COLLECTION":
                return R.drawable.ic_trade_merchant;
            case "VIP_RECHARGE":
                return R.drawable.ic_trade_recharge;
            case "WITHDRAW":
                return R.drawable.ic_trade_withdraw;
            case "REPAY_CONSUME":
            case "REPAY_PAYMENT":
            case "MANUAL_PAYMENT":
                return R.drawable.ic_trade_repay;
            default:
                return R.drawable.shape_placeholder;
        }
    }

    /**
     * 根据payType获取图片资源
     */
    public static int getMemberPayDrawable(String payType) {
        if (TextUtils.isEmpty(payType)) {
            return R.drawable.shape_placeholder;
        }
        switch (payType) {
            case "BALANCE":
                return R.drawable.ic_member_balance;
            case "ALIPAY":
                return R.drawable.ic_member_ali;
            case "QQ":
                return R.drawable.ic_pay_qq;
            case "WECHAT":
                return R.drawable.ic_member_wx;
            case "UNIONPAY":
            case "QUICK_PASS":
                return R.drawable.ic_pay_union;
//                return R.drawable.ic_pay_quick;
            default:
                return R.drawable.shape_placeholder;
        }
    }

    /**
     * 等级名称
     */
    public static String getLevelName(int levelId) {
        switch (levelId) {
            case 2:
                return "店家";
            case 3:
                return "账房";
            case 4:
                return "掌柜";
            default:
                return "客官";
        }
    }

    /**
     * 获取当月第一天
     */
    public static String getMonthFirstDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat(
                "yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.getTime();
        return dateFormater.format(cal.getTime());
    }

    /**
     * 提示弹框view
     */
    public static View generateHintView(Context mContext, int imageRes, String content) {
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        if (imageRes != 0) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(imageRes);
            layout.addView(imageView);
        }

        if (!TextUtils.isEmpty(content)) {
            TextView textView = new TextView(mContext);
            textView.setText(content);
            if (imageRes != 0) {
                textView.setPadding(0, ScreenUtil.dp2px(12), 0, 0);
            }
            layout.addView(textView);
        }
        return layout;
    }

    /**
     * 根据userGrade获取头像
     */
    public static int getHeaderViewDrawable(String userGrade) {
        if (TextUtils.isEmpty(userGrade)) {
            return R.drawable.ic_grade_normal;
        }
        switch (userGrade) {
            case CommonConfig.MEMBER_VIP:
                return R.drawable.ic_grade_vip;
            case CommonConfig.MEMBER_AMBASSADOR:
                return R.drawable.ic_grade_spreader;
            case CommonConfig.MEMBER_SERVICE:
                return R.drawable.ic_grade_operating;
            case CommonConfig.MEMBER_OPERATOR:
                return R.drawable.ic_grade_facilitator;
            default:
                return R.drawable.ic_grade_normal;
        }
    }

}
